<template>
	<view>
		<v-search @search="gotoSearch"></v-search>
		<view class="scroll">
			<!-- 左侧滚动 -->
			<scroll-view class="scroll-left" scroll-y="true" :style="{height:phoneHight + 'px'}">
				<view @tap="leftitem(index)" :class="['scroll-item',active === index ? 'active' : '']"  v-for="(item,index) in categorylist.slice(0,29)" :key="item.cat_id">
				{{item.cat_name}}
				</view>
			</scroll-view>
			<!-- 右侧滚动 -->
			<scroll-view :scroll-top="scrollTop" class="scroll-right" scroll-y="true" :style="{height:phoneHight + 'px'}">
				<view class="" v-for="(item2,i2) in cateLevel2" :key="item2.cat_id">
					<!-- 商品标题 -->
					<view class="title">{{item2.cat_name}}</view>
					<!-- 渲染商品数据 -->
					<view class="message">
						<view class="list" @tap="goodslist(item3)" v-for="(item3,i3) in item2.children" :key="i3">
							<!-- 商品图片 -->
							<view><u-image lazy-load="" width="60px" height="60px" :src="item3.cat_icon"></u-image></view>
							<!-- 商品名 -->
							<text>{{item3.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	import config from '../../Server/Config.js'
	import search from '../../components/search/search.vue'
	import http from '../../utils/http.js'
	export default {
		data() {
			return {
				phoneHight: '', //屏幕高度
				url:config.url,
				categorylist:[], //获取的数据
				active:0,  //默认索引
				cateLevel2:[],//右边的数据
				scrollTop:'', //滚动条距离顶部位置
				
			}
		},
		methods: {
			async getcategory(){
				const {data:res} = await http.get(`/api/public/v1/categories`)
				if(res.meta.status !== 200) return uni.$showMsg()
				this.categorylist = res.message
				this.cateLevel2 = res.message[0].children
			},//获取数据
			leftitem(i){
				this.active = i 
				this.cateLevel2 = this.categorylist[i].children
				  // 让 scrollTop 的值在 0 与 1 之间切换
				  this.scrollTop = this.scrollTop === 0 ? 1 : 0
			},//点击左侧某一项
			goodslist(item){
				uni.navigateTo({
					url:"/subpag/goods_list/goods_list?id=" + item.cat_id
				})
			},//点击某一项商品详情页面
			gotoSearch(){
				uni.navigateTo({
					url:'/subpag/search/search'
				})
			},//点击搜索
		},
		onLoad() {
			// 获取屏幕高度
			const height = uni.getSystemInfoSync()
			this.phoneHight  = height.windowHeight  - 50
			this.getcategory()
		},
		components:{
			"v-search":search
		}
	}
</script>

<style scoped lang="scss">
	.message{
		display: flex;
		flex-wrap: wrap;
	}
	.list{
		display: flex;
		flex-direction: column;
		width: 33.33%;
		margin-bottom: 10px;
		align-items: center;
		font-size: 24upx;
	}
	.title{
		  font-size: 12px;
		  font-weight: bold;
		  text-align: center;
		  padding: 15px 0;
	}
	.scroll {
		display: flex;
	}

	.scroll-left {
		width: 120px;
	}

	.scroll-item {
		line-height: 60px;
		background-color: #f7f7f7;
		text-align: center;
		font-size: 12px;

		&.active {
			background-color: #ffffff;
			position: relative;
			&::before{
				content: '';
				display: block;
				width: 3px;
				height: 30px;
				background-color: #c00000;
				position: absolute;
				left: 0;
				top: 50%;
				transform: translateY(-50%);
			}
		}
	}
</style>
